<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\admin\controller\Base;
use app\admin\model\LogModel;
use app\admin\model\LoginModel;
use app\admin\model\MenuRoleModel;
use app\admin\model\MenuModel;

class Login extends Base
{
	/**
	 * 登录对象
	 * @return mixed
	 */
	public $login_obj;
	
	/**
	 * 日志对象
	 * @return mixed
	 */
	public $log_obj;
	/**
	 * 栏目对象
	 * @return mixed
	 */
	public $menu_obj;
	/**
	 * 角色对象
	 * @return mixed
	 */
	public $menu_role_obj;
	/**
	 * 加密token
	 * @return mixed
	 */
	public $token;
	 /**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->token = TOKEN; 
		$this->login_obj = new LoginModel(); 
		$this->log_obj = new LogModel(); 
		$this->menu_obj = new MenuModel(); 
		$this->menu_role_obj = new MenuRoleModel(); 
	}
	/**
	 * 后台用户登录操作
	 * @return mixed
	 */
	public function logining()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$captcha = $input['captcha'];
			if(!captcha_check($captcha)){
				$msg = "验证码输入不正确！";
				$result = [
					'code' => 0,
					'msg'  => $msg,
					'list' => $input
				];
			}else{
				$input['token'] = $this->token;
				$ret = $this->login_obj->logining($input);
				if(!empty($ret)){
					if($ret['status'] != 1){
						$msg = "账号处于异常状态，请与管理员联系！";
						$result = [
							'code' => 0,
							'msg'  => $msg,
							'list' => $input
						];
					}else{
						Session::set('admin_id',$ret['id']);
						Session::set('user_name',$ret['user_name']);
						Session::set('role_id',$ret['role_id']);
						$this->log_obj->data([
							'user_id' =>  $ret['id'],
							'ip'   	  => getClientIp(),
							'type'    =>  1,
							'remark'  =>  '用户名:'.$ret['user_name'].'登录后台系统',
							'create_time'   =>  time()
						]);
						$this->log_obj->save();
						$this->login_obj->save([
							'login_time'  => time()
						],['id' => $ret['id']]);
						
						$result = [
							'code' => 1,
							'msg'  => '请求成功',
							'list' => $input
						];
					}
				}else{
					$msg = "账号或密码不正确！";
					$result = [
						'code' => 0,
						'msg'  => $msg,
						'list' => $input
					];
				}
			}
			return json($result);
		}else{
			$this->error('登录请求不合法！');
		}
	}
	
	/**
	 * 后台用户退出
	 * @return mixed
	 */
	public function loginout()
	{
		Session::delete('admin_id');
		Session::delete('user_name');
		Session::delete('role_id');
		$this->success('退出成功', '/'.MOUBLE_NAME.'/index/login');
	}	
	
	/**
	 * 获取用户登录的栏目导航
	 * @return mixed
	 */
	public function getUserNav()
	{
		$role_id = Session::get('role_id');
		//获取权限的栏目列表
		$menu_config = config('menu_list');
		$icon_config = config('icon_list');
		$menu_ids = array_keys($menu_config);
		$data_menu = array();
		$menu_list = array();
		array_push($menu_list,array('title'=>'后台首页','icon'=>'icon-computer','href'=>'','spread'=>false));
		if($role_id > 1){
			$where = array();
			$where['status'] = 0;
			$where['role_id'] = $role_id;
			$where['parent_id'] = 0;
			$role = $this->menu_role_obj->getRole($where);
			foreach($role as $k=>$v){
				$data_menu[$v['top_id']][$v['module_id']] = $v;
			}
			foreach($data_menu as $dk=>$dv){
				$data = array('title'=>$menu_config[$dk],'icon'=>$icon_config[$dk],'href'=>'','spread'=>false,'children'=>array());
				foreach($dv as $_dv){
					$where = array();
					$where['id'] = $_dv['module_id'];
					$menuRow = $this->menu_obj->getRow($where);
					$_data = array('title'=>$menuRow['name'],'icon'=>$menuRow['icon'],'href'=>'/'.MOUBLE_NAME.'/'.$menuRow['controller'].'/'.$menuRow['action'],'spread'=>false);
					array_push($data['children'],$_data);
				}
				array_push($menu_list,$data);
			}
		}else{
			$where = array();
			$where['status'] = 1;
			$where['parent_id'] = 0;
			$menu = $this->menu_obj->getList($where);
			foreach($menu as $k=>$v){
				$data_menu[$v['top_id']][$v['id']] = $v;
			}
			foreach($data_menu as $dk=>$dv){
				$data = array('title'=>$menu_config[$dk],'icon'=>$icon_config[$dk],'href'=>'','spread'=>false,'children'=>array());
				foreach($dv as $_dv){
					$_data = array('title'=>$_dv['name'],'icon'=>$_dv['icon'],'href'=>'/'.MOUBLE_NAME.'/'.$_dv['controller'].'/'.$_dv['action'],'spread'=>false);
					array_push($data['children'],$_data);
				}
				array_push($menu_list,$data);
			}
		}
		return json($menu_list);
	}
}
